#!/usr/bin/env python 
# -*- coding:utf-8 -*-
l = [36, 5, -12, 9, -21]
print(l)
print(sorted(l))
print(sorted(l, key=abs))    # 按照绝对值的大小

str_l = ['bob', 'about', 'Zoo', 'Credit']
print(str_l)
print(sorted(str_l))
print(sorted(str_l, key=str.upper))
print(sorted(str_l, key=str.lower))
print(sorted(str_l, key=str.lower, reverse=True))  # 倒序


l = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]


def by_name(tu):
    return str.upper(tu[0])


def by_score(tu):
    return tu[1]


l2 = sorted(l, key=by_name)
print(l2)

l3 = sorted(l, key=by_score)
print(l3)
l4 = sorted(l, key=by_score,reverse=False)
print(l4)
l5 = sorted(l, key=by_score,reverse=True)
print(l5)

L = [
    {
        "aqi": 20,
        "area": "上海",
        "pm2_5": 14,
        "pm2_5_24h": 14,
        "position_name": "十五厂",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1142 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 46,
        "area": "上海",
        "pm2_5": 32,
        "pm2_5_24h": 7,
        "position_name": "虹口",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1143 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 30,
        "area": "上海",
        "pm2_5": 19,
        "pm2_5_24h": 16,
        "position_name": "徐汇上师大",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1144 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 42,
        "area": "上海",
        "pm2_5": 15,
        "pm2_5_24h": 18,
        "position_name": "杨浦四漂",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1145 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 36,
        "area": "上海",
        "pm2_5": 20,
        "pm2_5_24h": 19,
        "position_name": "青浦淀山湖",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1146 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 29,
        "area": "上海",
        "pm2_5": 20,
        "pm2_5_24h": 17,
        "position_name": "静安监测站",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1147 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 51,
        "area": "上海",
        "pm2_5": 17,
        "pm2_5_24h": 16,
        "position_name": "浦东川沙",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "良",
        "station_code": "1148 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 29,
        "area": "上海",
        "pm2_5": 20,
        "pm2_5_24h": 11,
        "position_name": "浦东新区监测站",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1149 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 35,
        "area": "上海",
        "pm2_5": 24,
        "pm2_5_24h": 20,
        "position_name": "浦东张江",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1150 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 36,
        "area": "上海",
        "pm2_5": 17,
        "pm2_5_24h": 14,
        "position_name": "普陀",
        "primary_pollutant": "颗粒物(PM10)",
        "quality": "优",
        "station_code": "1141 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    },
    {
        "aqi": 34,
        "area": "上海",
        "pm2_5": 19,
        "pm2_5_24h": 15,
        "position_name": "wangnan",
        "primary_pollutant": "",
        "quality": "优",
        "station_code": "1141 A",
        "time_point": "2017 - 07 - 29 T14: 00: 00 Z"
    }
]

def by_aqi(dic):
   return dic['aqi']


L1 = sorted(L, key=by_aqi, reverse=True)
for i in L1:
    print(i)